<?php
class Sb_Controller_Plugin_View extends  Sb_Controller_Plugin_Base
{
	
	const PATH_TO_VIEW_INI = '/configs/view.ini';
	
	
	public function dispatchLoopStartup( Zend_Controller_Request_Abstract $request )
	{
		$_viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper( 'ViewRenderer' );
		$_viewRenderer->init( );
		try{
			$_config = new Zend_Config_Ini( APPLICATION_PATH.self::PATH_TO_VIEW_INI );
			$_viewRenderer->view->headMeta()->appendHttpEquiv("Content-Type","text/html; charset=UTF-8");
			$this->appendStylesheet($_viewRenderer->view,$_config->css->toArray())
					->appendJs($_viewRenderer->view,$_config->js->toArray());
		}catch(Zend_Config_Exception $_exp){
			Sb_Debug::debug($_exp->getMessage( ) );
		}
	}
	
	protected function appendStylesheet(Zend_View $_view, array $files)
	{
		$_headLink = $_view->headLink( );
		$baseUrl = $_view->baseUrl( );
		foreach($files as $file){
			if(false === strpos($file,'http')){
				$file = $baseUrl.$file;
			}
			$_headLink->appendStylesheet( $file );
		}
		return $this;
	}
	
	protected function appendJs( Zend_View $_view, array $files )
	{
		$_headScript = $_view->headScript( );
		$baseUrl = $_view->baseUrl( );
		foreach($files as $file){
			if(false === strpos($file,'http')){
				$file = $baseUrl.$file;
			}
			$_headScript->appendFile( $file );
		}
		return $this;
	}
}


